package com.plus.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ZipUtil;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @program: plus-easy
 * @ClassName GzipUtil
 * @description:缓存的数据量很大时压缩数据后缓存..
 * @author: 黄涛
 * @create: 2023-12-20 11:38
 * @Version 1.0
 **/
public class GzipUtil {


    /**
     * 使用gzip压缩字符串
     * @param str 要压缩的字符串
     * @return
     */
    public static String compress(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        byte[] bytes = ZipUtil.gzip(str.getBytes());
        String compressStr = new BASE64Encoder().encode(bytes);
        return compressStr;
    }

    /**
     * 使用gzip解压缩
     * @param compressedStr 压缩字符串
     * @return
     */
    public static String uncompress(String compressedStr) {
        if (compressedStr == null || compressedStr.length() == 0) {
            return compressedStr;
        }
        byte[] bytes = new byte[0];
        try {
            bytes = new BASE64Decoder().decodeBuffer(compressedStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] uncompressStr = ZipUtil.unGzip(bytes);
        return new String(uncompressStr);
    }

}
